<template>
  <div>
    <top current="tsd"></top>
    <div style="width:1200px; margin:0 auto; min-height: calc(100vh - 90px);background: white">
      <select-bank v-on:selected-bank="selectedBank"></select-bank>
    </div>
  </div>
</template>
<script>

import Top from '@/components/layout/top';
import selectBank from "@/components/process/selectBank";
import {eventTracking} from '@/api/plat/plat';
import {ztLogin} from '@/api/login/login';

export default {
  name: 'productList',
  components: {
    Top,selectBank
  },
  destroyed (){
    localStorage.removeItem('ztFlag');
    window.removeEventListener('beforeunload',()=>{});
    window.removeEventListener('unload',()=>{});
  },
  mounted(){
    window.addEventListener('beforeunload', e => this.unloadHandler(e))
    window.addEventListener('unload', e => this.unloadHandler(e))
    this.track("tiaozhuan");
  },
  methods:{
    unloadHandler(){
      localStorage.removeItem('ztFlag')
    },
    /***
     * 埋点
     * @param operateName
     */
    track(operateName,productCode,name){
      let params;
      let source;
      let channel = this.queryString('channel');
      let authCode = this.queryString('authCode');

      if (channel=='zt_other' || channel=='zt_banner') {
        source = (channel=='zt_other')?'中天菜单':'中天banner';

        if (operateName=='tiaozhuan') {
          params = {
            "appCode": "wmr",
            "operateName": operateName,
            "productCode": productCode || "",
            "source": source,
          };

          if (localStorage.getItem('ztFlag')!='1') {
            eventTracking(params).then(res=>{
              localStorage.setItem('ztFlag','1')
              this.singleLogin(authCode);
            });
          }
        } else {

          if (name) {
            source = source + '+' + name;
          }
          params = {
            "appCode": "wmr",
            "operateName": operateName,
            "productCode": productCode || "",
            "source": source,
          };
          eventTracking(params).then(res=>{});
        }
      }
      else {
        if (name) {
          params = {
            "appCode": "wmr",
            "operateName": operateName,
            "productCode": productCode || "",
            "source": name || "",
          };
          eventTracking(params).then(res=>{});
        }
      }
    },
    /***
     * 单点登录
     * 静默
     * @param operateName
     */
    singleLogin(authCode){
      ztLogin(authCode).then(res=>{
        if (res.code == 200) {
          if (res.data){
            this.$store.commit('LOGIN_SUCCESS', res.data);
          }
        }
      });
    },
    queryString(name) {
      let after = window.location.search;
      after = after.substr(1) || window.location.hash.split('?')[1];
      // 地址栏URL没有查询参数，返回空
      if (!after) return null;
      // 如果查询参数中没有"name"，返回空
      if (after.indexOf(name) === -1) return null;

      let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
      // 当地址栏参数存在中文时，需要解码，不然会乱码
      // let r = decodeURI(after).match(reg);
      let r = after.match(reg);
      // 如果url中"name"没有值，返回空
      if (!r) return null;

      return r[2];
    },
    selectedBank(result){
      this.track('dianji',result.productCode,result.name);
      this.$router.push({
        name: 'productDetail',
        query: result
      });
    }
  }
}
</script>

